package formServlets;
import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import java.io.IOException;
import java.util.Iterator;

//import guestbook.PMF;

public class SignUpServlet extends HttpServlet  {

	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		getServletContext().getRequestDispatcher("SNSP.html").forward(req, resp);
	}
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		System.out.println("You Have Entered username: " + username + " password " + password+" email: "+email);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		Entity user = new Entity("UserSignUp",username);
		user.setProperty("Username", username);
		user.setProperty("Password", password);
		user.setProperty("email", email);

		datastore.put(user);
		//Iterator<Key> k=datastore.
		
		try {
			Key key = KeyFactory.createKey("UserSignUp", username);
			Entity e=datastore.get(key);
			System.out.print(e.getProperties());
			System.out.print(e.getProperty("Password"));
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DatastoreAttributes ds=datastore.getDatastoreAttributes();
		DatastoreAttributes.DatastoreType dst= ds.getDatastoreType();
		System.out.print(dst.name());
		
		//Cookie[] cookies = request.getCookies();
        Cookie c = new Cookie("passUsername", username);
        response.addCookie(c); 
        
		//java.util.Map<Key,Entity> entities=	datastore.get(keys);
		//System.out.print("here");
		//System.out.println(entities.isEmpty());
		response.sendRedirect("successfulSignup.html");
 
	}
 
}
